using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;

public partial class Pages_AEditFeature : Mgh.UI.Sql.PageBase
{
    #region properties

    private string FeaID
    {
        get { return Request.QueryString["feid"] == null ? string.Empty : Request.QueryString["feid"]; }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(FeaID))
                this.bindData();
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DataSources.Jw_Tbl_FeatureDataTable tb = this.prepairData();
            ProductBLL bn = new ProductBLL(getCurrentConnect());
            if (tb != null)
            {
                if (string.IsNullOrEmpty(this.FeaID))
                {
                    if (!bn.addFeatures(tb))
                    {
                        cval.IsValid = false;
                        cval.Text = bn.getMessage();
                    }
                    else Response.Redirect("AEditFeature.aspx");
                }
                else
                {
                    if (!bn.updateFeatures(tb))
                    {
                        cval.IsValid = false;
                        cval.Text = bn.getMessage();
                    }
                    else Response.Redirect("AManageFeature.aspx");
                }
            }
        }
    }

    #endregion

    #region functions

    private DataSources.Jw_Tbl_FeatureDataTable prepairData()
    {
        DataSources.Jw_Tbl_FeatureDataTable tb = new DataSources.Jw_Tbl_FeatureDataTable();
        try
        {
            DataSources.Jw_Tbl_FeatureRow row = tb.NewJw_Tbl_FeatureRow();
            row.FeaID = this.FeaID;
            row.Ten = txtName.Text;
            row.MoTa = edDesc.Text;
            tb.AddJw_Tbl_FeatureRow(row);
            return tb;
        }
        catch (Exception ex) { cval.IsValid = false; cval.Text = ex.Message; return null; }
        
    }

    private void bindData()
    {
        ProductBLL bn = new ProductBLL(getCurrentConnect());
        DataSources.Jw_Tbl_FeatureDataTable tb = bn.getFeature(this.FeaID);
        if (tb != null)
        {
            txtName.Text = tb[0].IsTenNull() ? string.Empty : tb[0].Ten;
            edDesc.Text = tb[0].IsMoTaNull() ? string.Empty : tb[0].MoTa;
            //txtDesc.Text = tb[0].IsMotaNull() ? string.Empty : tb[0].Mota;
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }

    }

    #endregion
}
